package control.member;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.member.MemberService;
import model.member.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MemberController extends MultiActionController{
	private MemberService memberService;
	public void setMemberService(MemberService memberService){
		this.memberService=memberService;
	}
	public ModelAndView loginForm(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Cookie[] ca=request.getCookies();
		String id=null;
		if(ca!=null){
			for(int i=0; i<ca.length; i++){
				if(ca[i].getName().equals("id")){
					id=ca[i].getValue();
				}
			}
		}
		System.out.println("Asdadsfdsfdsadfaf"+id);
		return new ModelAndView("member_view/login","id", id);
	}
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response,MemberVO pvo) throws Exception{
		String path="member_view/login_fail";
		String saveId=request.getParameter("saveId");
		if(saveId!=null){
			Cookie cookie=new Cookie("id", pvo.getId());
			cookie.setMaxAge(3600*24*30);//쿠키 유효기간 1달
			response.addCookie(cookie);
		}else{
			Cookie[] ca=request.getCookies();
			if(ca!=null){
				for(int i=0; i<ca.length; i++){
					if(ca[i].getName().equals("id")){
						Cookie cookie=new Cookie("id", "");
						cookie.setMaxAge(0);//쿠키 유효기간 0초
						response.addCookie(cookie);
					}
				}
			}
		}
		//System.out.println(pvo);
		MemberVO rvo=memberService.login(pvo);
		if(rvo!=null){
			request.getSession().setAttribute("vo", rvo);
			path="member_view/login_ok";
		}
		return new ModelAndView(path);
	}
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response,HttpSession session) throws Exception{
		if(session!=null)
			session.invalidate();
		return new ModelAndView("index");
	}
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,MemberVO vo) throws Exception{
		/*System.out.println("updateMember"+vo);*/
		memberService.update(vo);
		HttpSession session = request.getSession();
		if(session!=null){
			session.setAttribute("vo",vo);
		}
		//System.out.println(vo);
		return new ModelAndView("member_view/modify_ok","vo",vo);
	}
	public ModelAndView idCheck(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String id=request.getParameter("id");
		System.out.println(id);
		String mess=memberService.idCheck(id);
		System.out.println(mess);
		return new ModelAndView("JsonView","mess",mess);
	}
	
	public ModelAndView nickCheck(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String nick=request.getParameter("nick");
		System.out.println(nick);
		String mess2=memberService.nickCheck(nick);
		System.out.println(mess2);
		return new ModelAndView("JsonView","mess2",mess2);
	}
	
	public ModelAndView registerMember(HttpServletRequest request,HttpServletResponse response,MemberVO vo) throws Exception{
		System.out.println("registerMember"+vo);
		memberService.registerMember(vo);
		return new ModelAndView("member_view/register_confirm","vo",vo);
	}
	
	public ModelAndView notDuplicate(HttpServletRequest request,HttpServletResponse response,String uri) throws SQLException{
		HashMap map=memberService.notDuplicate(uri);
		System.out.println("컨트롤러 - notDuplicate - "+map);
		return new ModelAndView("member_view/regit_certify","map",map);
	}
}
